package com.taotao.sso.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.JsonUtils;
import com.taotao.jedis.JedisClient;
import com.taotao.mapper.TbUserMapper;
import com.taotao.pojo.TbUser;
import com.taotao.pojo.TbUserExample;
import com.taotao.pojo.TbUserExample.Criteria;
import com.taotao.sso.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private TbUserMapper userMapper;
	
	@Autowired
	private JedisClient	jedisClient;
	
	@Value("${USER_INFO}")
	private String USER_INFO;
	
	@Value("${EXPIRE_TIME}")
	private Integer EXPIRE_TIME;
	@Override
	public TaotaoResult verifyInfo(String info, Integer type) {
		
		TbUserExample example = new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(type == 1){
			criteria.andUsernameEqualTo(info);
		}else if(type == 2){
			criteria.andPhoneEqualTo(info);
		}else if(type == 3){
			criteria.andEmailEqualTo(info);
		}else{
			return TaotaoResult.ok("用户登录异常!");
		}
		List<TbUser> list = userMapper.selectByExample(example);
		if(list == null || list.size() == 0){
			return TaotaoResult.ok(true);
		}
		return TaotaoResult.ok(false);
	}
	@Override
	public TaotaoResult registerUser(TbUser user) {

		// 1、使用TbUser接收提交的请求。
		
		if (StringUtils.isBlank(user.getUsername())) {
			return TaotaoResult.build(400, "用户名不能为空");
		}
		if (StringUtils.isBlank(user.getPassword())) {
			return TaotaoResult.build(400, "密码不能为空");
		}
		//校验数据是否可用
		TaotaoResult result = verifyInfo(user.getUsername(), 1);
		if (!(boolean) result.getData()) {
			return TaotaoResult.build(400, "此用户名已经被使用");
		}
		//校验电话是否可以
		if (StringUtils.isNotBlank(user.getPhone())) {
			result = verifyInfo(user.getPhone(), 2);
			if (!(boolean) result.getData()) {
				return TaotaoResult.build(400, "此手机号已经被使用");
			}
		}
		//校验email是否可用
		if (StringUtils.isNotBlank(user.getEmail())) {
			result = verifyInfo(user.getEmail(), 3);
			if (!(boolean) result.getData()) {
				return TaotaoResult.build(400, "此邮件地址已经被使用");
			}
		}
		// 2、补全TbUser其他属性。
		user.setCreated(new Date());
		user.setUpdated(new Date());
		// 3、密码要进行MD5加密。
		String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
		user.setPassword(md5Pass);
		// 4、把用户信息插入到数据库中。
		userMapper.insert(user);
		// 5、返回TaotaoResult。
		return TaotaoResult.ok();

	}
	@Override
	public TaotaoResult login(String username, String password) {
		String token = null;
		if(username != null){
			TbUserExample example = new TbUserExample();
			Criteria criteria = example.createCriteria();
			criteria.andUsernameEqualTo(username);
			List<TbUser> list = userMapper.selectByExample(example);
			
			if(list == null || list.size() == 0){
				return TaotaoResult.build(400,"用户名或密码错误!");
			}
			
			TbUser user = list.get(0);
			
			if(!user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
				return TaotaoResult.build(400,"用户名或密码错误!");
			}
			
			token = UUID.randomUUID().toString();
			
			user.setPassword(null);
			jedisClient.set(USER_INFO+":"+token, JsonUtils.objectToJson(user));
			jedisClient.expire(USER_INFO+":"+token, EXPIRE_TIME);
		}
		return TaotaoResult.ok(token);
	}
	
	@Override
	public TaotaoResult checkToken(String token) {
		String json = jedisClient.get(USER_INFO+":"+token);
		if(StringUtils.isBlank(json) ){
			return TaotaoResult.build(400, "用户未登录或登录信息已过期!");
		}
		
		jedisClient.expire(USER_INFO+":"+token, EXPIRE_TIME);
		TbUser user = JsonUtils.jsonToPojo(json, TbUser.class);
		return TaotaoResult.ok(user);
	}
	
	@Override
	public TaotaoResult clearToken(String cOOKIE_TOKEN_KEY) {
		jedisClient.expire(cOOKIE_TOKEN_KEY, 1);
		return TaotaoResult.ok("删除token成功!");
	}

}
